package GUI;

import MachineLogic.Ticket;
import java.awt.Color;
import java.io.*;

/**
 * Class for the Setup2 panel in GUI
 */
public class Setup2 extends javax.swing.JPanel {
    tvmGUI master;
    Setup1 Setup1Class;
    WelcomeDA WelcomeDAClass;
    Admin AdminClass;
    int timeoutTime = 10000;    // 10 seconds without mouse movement = timeout 
    int timoutInitDelay = 10000; // Begin mouse movement check after 10 seconds
    int timoutCheckPeriod = 60000; // Check timoutTimer every 60 seconds 
    
    /**
     * Custom constructor
     * @param tvmGUI Reference to {@link tvmGUI}.
     */
    public Setup2(tvmGUI tvmGUI) {
        master = tvmGUI;
        initComponents();
    }
    
    /**
     * Sets references to other panels
     * @param refSetup1 Reference to Setup1 panel.
     * @param refWelcomeDA Reference to WelcomeDA panel.
     * @param refAdmin Reference to Admin panel.
     */
    public void setReferences(Setup1 refSetup1, WelcomeDA refWelcomeDA
            , Admin refAdmin) {
        Setup1Class = refSetup1;
        WelcomeDAClass = refWelcomeDA;
        AdminClass = refAdmin;
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        LabSetup2Info = new javax.swing.JLabel();
        LabSetup2Time = new javax.swing.JLabel();
        LabSetup2Title = new javax.swing.JLabel();
        LabSetup2TypeDA = new javax.swing.JLabel();
        InSetup2TypeDA = new javax.swing.JTextField();
        LabSetup2TypeENG = new javax.swing.JLabel();
        InSetup2TypeENG = new javax.swing.JTextField();
        LabSetup2PricePZ = new javax.swing.JLabel();
        InSetup2PricePZ = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        ListSetup2TicketList = new javax.swing.JList();
        LabSetup2TicketList = new javax.swing.JLabel();
        ButSetup2AddM = new javax.swing.JButton();
        ButSetup2Save = new javax.swing.JButton();
        ButSetup2Back = new javax.swing.JButton();
        LabSetup2Error = new javax.swing.JLabel();
        LabSetup2ErrorList = new javax.swing.JLabel();
        BackgroundPIC = new javax.swing.JLabel();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());
        add(LabSetup2Info, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        LabSetup2Time.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        LabSetup2Time.setText(" ");
        add(LabSetup2Time, new org.netbeans.lib.awtextra.AbsoluteConstraints(594, 11, 196, -1));

        LabSetup2Title.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        LabSetup2Title.setText("Opret billetter");
        add(LabSetup2Title, new org.netbeans.lib.awtextra.AbsoluteConstraints(317, 31, -1, -1));

        LabSetup2TypeDA.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabSetup2TypeDA.setText("Indtast navnet på billettypen på dansk.");
        add(LabSetup2TypeDA, new org.netbeans.lib.awtextra.AbsoluteConstraints(477, 115, 198, -1));
        add(InSetup2TypeDA, new org.netbeans.lib.awtextra.AbsoluteConstraints(477, 135, 198, -1));

        LabSetup2TypeENG.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabSetup2TypeENG.setText("Indtast navnet på billettypen på engelsk.");
        add(LabSetup2TypeENG, new org.netbeans.lib.awtextra.AbsoluteConstraints(477, 173, -1, -1));
        add(InSetup2TypeENG, new org.netbeans.lib.awtextra.AbsoluteConstraints(477, 198, 198, -1));

        LabSetup2PricePZ.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabSetup2PricePZ.setText("Indtast pris pr. zone.");
        add(LabSetup2PricePZ, new org.netbeans.lib.awtextra.AbsoluteConstraints(477, 236, 198, -1));
        add(InSetup2PricePZ, new org.netbeans.lib.awtextra.AbsoluteConstraints(477, 261, 198, -1));

        ListSetup2TicketList.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        ListSetup2TicketList.setFocusable(false);
        ListSetup2TicketList.setVisibleRowCount(10);
        jScrollPane1.setViewportView(ListSetup2TicketList);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(47, 135, 302, 171));

        LabSetup2TicketList.setText("Oprettede billetter.");
        add(LabSetup2TicketList, new org.netbeans.lib.awtextra.AbsoluteConstraints(47, 115, 192, -1));

        ButSetup2AddM.setText("Tilføj flere");
        ButSetup2AddM.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButSetup2AddMActionPerformed(evt);
            }
        });
        add(ButSetup2AddM, new org.netbeans.lib.awtextra.AbsoluteConstraints(690, 545, 85, 31));

        ButSetup2Save.setText("Gem og afslut");
        ButSetup2Save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButSetup2SaveActionPerformed(evt);
            }
        });
        add(ButSetup2Save, new org.netbeans.lib.awtextra.AbsoluteConstraints(573, 545, -1, 31));

        ButSetup2Back.setText("Tilbage");
        ButSetup2Back.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButSetup2BackActionPerformed(evt);
            }
        });
        add(ButSetup2Back, new org.netbeans.lib.awtextra.AbsoluteConstraints(464, 545, 91, 31));

        LabSetup2Error.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        LabSetup2Error.setForeground(new java.awt.Color(255, 0, 0));
        LabSetup2Error.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        LabSetup2Error.setText("Fejl - Ugyldig indtastning");
        add(LabSetup2Error, new org.netbeans.lib.awtextra.AbsoluteConstraints(516, 292, -1, -1));

        LabSetup2ErrorList.setFont(new java.awt.Font("Tahoma", 2, 11)); // NOI18N
        LabSetup2ErrorList.setForeground(new java.awt.Color(255, 0, 0));
        LabSetup2ErrorList.setText("Fejl - Ingen billetter oprettet");
        add(LabSetup2ErrorList, new org.netbeans.lib.awtextra.AbsoluteConstraints(128, 317, -1, -1));

        BackgroundPIC.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/BackgroundGUI.png"))); // NOI18N
        add(BackgroundPIC, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 810, 600));
    }// </editor-fold>//GEN-END:initComponents
    
    private void ButSetup2AddMActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButSetup2AddMActionPerformed
        String TypeDA = InSetup2TypeDA.getText().trim();                      
        String TypeENG = InSetup2TypeENG.getText().trim();
        int PricePZ = master.isInteger(InSetup2PricePZ.getText().trim());
        master.checkFieldsSetup2(PricePZ, TypeDA, TypeENG);
        LabSetup2ErrorList.setVisible(false);
        if (PricePZ !=-1 && !TypeDA.equals("") && !TypeENG.equals("")) {
            master.addTicketToListSetup2(PricePZ, TypeDA, TypeENG);
        }
    }//GEN-LAST:event_ButSetup2AddMActionPerformed

    private void ButSetup2SaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButSetup2SaveActionPerformed
        String TypeDA = InSetup2TypeDA.getText().trim();                     
        String TypeENG = InSetup2TypeENG.getText().trim();
        int PricePZ = master.isInteger(InSetup2PricePZ.getText());
        if (ListSetup2TicketList.getModel().getSize() == 0) {
            if (PricePZ !=-1 && !TypeDA.equals("") && !TypeENG.equals("")) {
                master.addTicketToListSetup2(PricePZ, TypeDA, TypeENG);
                writeConf();
                master.ChangePanel(WelcomeDAClass);
                master.runTimeOut(timeoutTime, timoutInitDelay
                        , timoutCheckPeriod);    // activate tmeout possibility
                WelcomeDAClass.LabWelcomeDAInfo.setText("Maskin ID: "
                        +master.hardID+"  Zone: "+master.startZone);
                AdminClass.LabAdminInfo.setText("Maskin ID: "+master.hardID
                        +"  Zone: "+master.startZone);
                return;
            } else {
                LabSetup2ErrorList.setVisible(true);
            }
        } else {
            LabSetup2ErrorList.setVisible(false);
        }
        master.checkFieldsSetup2(PricePZ, TypeDA, TypeENG);
        if (!master.ListContent.isEmpty()) {
            if (PricePZ ==-1 && TypeDA.equals("") && TypeENG.equals("")) {
                master.ChangePanel(WelcomeDAClass);
                master.runTimeOut(timeoutTime, timoutInitDelay
                        , timoutCheckPeriod);    // activate tmeout possibility
            } else if (PricePZ !=-1 && !TypeDA.equals("") 
                    && !TypeENG.equals("")) {
                master.addTicketToListSetup2(PricePZ, TypeDA, TypeENG);
                writeConf();
                master.ChangePanel(WelcomeDAClass);
                master.runTimeOut(timeoutTime, timoutInitDelay
                        , timoutCheckPeriod);    // activate tmeout possibility
                WelcomeDAClass.LabWelcomeDAInfo.setText("Maskin ID: "
                        +master.hardID+"  Zone: "+master.startZone);
            } else {
                master.checkFieldsSetup2(PricePZ, TypeDA, TypeENG);
            }
        }
    }//GEN-LAST:event_ButSetup2SaveActionPerformed

    @SuppressWarnings("unchecked")
    private void ButSetup2BackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButSetup2BackActionPerformed
        //Returning all warning labels to default
        LabSetup2TypeENG.setForeground(Color.black);
        LabSetup2TypeDA.setForeground(Color.black);
        LabSetup2PricePZ.setForeground(Color.black);
        LabSetup2Error.setVisible(false);
        LabSetup2ErrorList.setVisible(false);
        // Get number of items in typeCombobox; -1 to ignore first element
        int CBWItemCount = WelcomeDAClass.CBWelcomeDAType.getItemCount()-1;
        // Delete every other item than the first
        for (;CBWItemCount > 0 ; CBWItemCount--) {
            WelcomeDAClass.CBWelcomeDAType.removeItemAt(CBWItemCount);
        }
        //Clear array of Strings for Jlist
        master.ListContent.clear();
        // Set Jlist with empty array
        ListSetup2TicketList.setListData(master.ListContent.toArray());
        // Clear array of created tickets
        master.CT.ClearArray();
        //Change window to previous window
        master.ChangePanel(Setup1Class);
    }//GEN-LAST:event_ButSetup2BackActionPerformed

    /**
     * Writes a log file with the information entered during manual setup.
     */
    private void writeConf() {
        PrintWriter conf;
        try  {
            conf = new PrintWriter(new BufferedWriter(
                    new FileWriter("tvm.setup",false)));
            conf.println(""+ master.hardID);
            conf.println(""+ master.startZone);
            conf.println("10000"); // timeout margin, ns
            conf.println("10000"); // initial timeout delay, ns
            conf.println("60000"); // period for timeout checks, ns
            for (Ticket t : master.CT.getArray()) {
                    conf.print(+t.getPricePerZone()+":");
                    conf.print(t.getTypeDA()+":");
                    conf.println(t.getTypeENG());
            }
            conf.close();
        }
        catch (IOException ex) {
            System.err.println("Der kunne ikke skrives en setup fil.");
        }      
    } 

    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JLabel BackgroundPIC;
    public javax.swing.JButton ButSetup2AddM;
    public javax.swing.JButton ButSetup2Back;
    public javax.swing.JButton ButSetup2Save;
    public javax.swing.JTextField InSetup2PricePZ;
    public javax.swing.JTextField InSetup2TypeDA;
    public javax.swing.JTextField InSetup2TypeENG;
    public javax.swing.JLabel LabSetup2Error;
    public javax.swing.JLabel LabSetup2ErrorList;
    public javax.swing.JLabel LabSetup2Info;
    public javax.swing.JLabel LabSetup2PricePZ;
    public javax.swing.JLabel LabSetup2TicketList;
    public javax.swing.JLabel LabSetup2Time;
    public javax.swing.JLabel LabSetup2Title;
    public javax.swing.JLabel LabSetup2TypeDA;
    public javax.swing.JLabel LabSetup2TypeENG;
    public javax.swing.JList ListSetup2TicketList;
    public javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
}
